/**
 * Format date string to localized format
 * @param {string|Date} date - Date object or date string
 * @param {Object} options - Formatting options
 * @returns {string} Formatted date string
 */
export function formatDate(date, options = {}) {
  if (!date) return ''
  
  const defaultOptions = {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  }
  
  const dateObj = date instanceof Date ? date : new Date(date)
  
  // Use provided options or defaults
  const formatOptions = Object.keys(options).length > 0 ? options : defaultOptions
  
  return dateObj.toLocaleString('zh-CN', formatOptions)
}

/**
 * Format date to YYYY-MM-DD
 * @param {string|Date} date - Date object or date string
 * @returns {string} Formatted date string
 */
export function formatDateYMD(date) {
  if (!date) return ''
  
  const dateObj = date instanceof Date ? date : new Date(date)
  
  const year = dateObj.getFullYear()
  const month = String(dateObj.getMonth() + 1).padStart(2, '0')
  const day = String(dateObj.getDate()).padStart(2, '0')
  
  return `${year}-${month}-${day}`
}

/**
 * Get relative time (e.g., "3 days ago")
 * @param {string|Date} date - Date object or date string
 * @returns {string} Relative time string
 */
export function getRelativeTime(date) {
  if (!date) return ''
  
  const dateObj = date instanceof Date ? date : new Date(date)
  const now = new Date()
  
  const diffInSeconds = Math.floor((now - dateObj) / 1000)
  
  if (diffInSeconds < 60) {
    return `${diffInSeconds}秒前`
  }
  
  const diffInMinutes = Math.floor(diffInSeconds / 60)
  if (diffInMinutes < 60) {
    return `${diffInMinutes}分钟前`
  }
  
  const diffInHours = Math.floor(diffInMinutes / 60)
  if (diffInHours < 24) {
    return `${diffInHours}小时前`
  }
  
  const diffInDays = Math.floor(diffInHours / 24)
  if (diffInDays < 30) {
    return `${diffInDays}天前`
  }
  
  const diffInMonths = Math.floor(diffInDays / 30)
  if (diffInMonths < 12) {
    return `${diffInMonths}个月前`
  }
  
  const diffInYears = Math.floor(diffInMonths / 12)
  return `${diffInYears}年前`
} 